<template>
  <a-card :bordered="false">

    <!-- 查询区域-start -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="refreshTable">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="订单号">
              <a-input placeholder="订单号" v-model="queryParam.serialNo" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="12">
            <a-form-item label="订单号">
              <a-select placeholder="订单状态" v-model="queryParam.status">
                <a-select-option v-for="(item,index) in orderStatusEnum" :key="index" :value="item.KEY">
                  {{ item.NAME }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="refreshTable" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-end -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="add" type="primary" icon="plus">创建订单</a-button>
    </div>

    <!-- table区域 -->
    <div>

      <s-table
        ref="table"
        rowKey="id"
        :columns="columns"
        :data="loadData"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="start(record)">开始</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定结束该订单吗?" @confirm="() => finish(record.id)">
            <a>结束</a>
          </a-popconfirm>
          <a-divider type="vertical"/>
          <a @click="afterSale(record)">售后</a>
          <a-divider type="vertical"/>
          <a @click="edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => del(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </div>

    <order-modal ref="modal" @ok="refreshTable"></order-modal>
    <after-sale-list ref="afterSaleList"></after-sale-list>
    <after-sale-modal ref="afterSaleModal"></after-sale-modal>
  </a-card>
</template>

<script>
import { ListMixin } from '@/mixins/ListMixin'
import { apiQueryOrderList, apiFinishOrder } from '@/api/order'
import OrderModal from './modules/OrderModal'
import AfterSaleList from './modules/AfterSaleList'
import AfterSaleModal from './modules/AfterSaleModal'
import { ORDER_STATUS_ENUM } from '@/store/mutation-types'

const columns = [
  {
    title: '订单号',
    dataIndex: 'serialNo',
    key: 'serialNo'
  },
  {
    title: '商品信息',
    dataIndex: 'commodityName',
    key: 'commodityName'
  },
  {
    title: '状态',
    dataIndex: 'statusDesc',
    key: 'statusDesc'
  },
  {
    title: '日期',
    dataIndex: 'crtDt',
    key: 'crtDt'
  },
  {
    title: '金额',
    dataIndex: 'amountDesc',
    key: 'amountDesc'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    width: 250
  }
]

export default {
  name: 'OrderList',
  mixins: [ListMixin],
  components: {
    OrderModal, AfterSaleList, AfterSaleModal
  },
  data () {
    return {
      columns,
      loadData: parameter => {
        return apiQueryOrderList(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      },
      orderStatusEnum: ORDER_STATUS_ENUM
    }
  },
  methods: {
    add () {
      this.$refs.modal.show('新增')
    },
    edit (record) {
      this.$refs.modal.show('修改', record)
    },
    afterSale (record) {
      this.$refs.afterSaleList.show(`订单[${record.serialNo}]的售后记录`, record)
    },
    start (record) {
      this.$refs.afterSaleModal.show(`开始处理订单[${record.serialNo}]`, { orderId: record.id })
    },
    finish (id) {
      apiFinishOrder(id).then(res => {
        this.$message.success(res.msg)
      }).finally(() => {
        this.refreshTable()
      })
    }
  }
}
</script>

<style scoped>

</style>
